package fun.wlfj.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import fun.wlfj.factory.CashSystemFactory;
import fun.wlfj.vo.Product;

/**
 * Servlet implementation class ProductInfoUpdateServlet
 */
@WebServlet("/ProductInfoUpdateServlet")
public class ProductInfoUpdateServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ProductInfoUpdateServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int ProductID = Integer.parseInt(request.getParameter("ProductID"));
		request.setAttribute("ProductModifyInfo", CashSystemFactory.getProductDao().getProductByID(ProductID));
		request.getRequestDispatcher("productManage.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		// 首先把信息都拿上 创建对象后调用方法更新
		int ProductID = Integer.parseInt(request.getParameter("ProductID"));
		String ProductName = request.getParameter("ProductName");
		String ProductShortName = request.getParameter("ProductShortName");
		int ProductNums = Integer.parseInt(request.getParameter("ProductNums"));
		double ProductPrice = Double.parseDouble(request.getParameter("ProductPrice"));
		Product p = new Product(ProductID, ProductNums, ProductName, ProductShortName, ProductPrice);
		CashSystemFactory.getProductDao().modifyProduct(p);
		response.sendRedirect("productManage.jsp");
	}

}
